using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Xml;

namespace csammisrun.shaim
{
  partial class Core
  {
    private const string UIPluginPathFormat = "{0}\\plugins\\ui\\{1}";
    private const string DEFAULTUISET = "DefaultUI.dll";
    private const string FUNCTIONALITYPATHFORMAT = "{0}\\plugins\\functionality\\{1}";
    private const string LOGENTRYHEADER = "{0} ({1}):";
    private const string ACCOUNTS = "/shaim/" + CORESETTINGS + "/Accounts";
    private const string CORESETTINGS = "Core";

    private static ICore _self = null;
    private ShaimAccount _account = new ShaimAccount();
    private List<ShaimFunctionalityInfo> _functionalityplugins = new List<ShaimFunctionalityInfo>();
    private ShaimStatusMessage _currentstatusmessage = null;
    private ShaimContactList _contactlist = null;
    private SortedList<int, List<PluginHandler>> _incomingevents = new SortedList<int, List<PluginHandler>>();
    private SortedList<int, List<PluginHandler>> _outgoingevents = new SortedList<int, List<PluginHandler>>();
    private TextWriter _logstream = null;
    private ShaimLogLevel _loglevel = ShaimLogLevel.Debug;
    private string _settingspath = "";
    private string _shaimdir = "";
    private XmlDocument _settings = new XmlDocument();
    private ShaimProxyInfo _proxyinfo = null;
    private IUIPlugin2 _uiset = null;

    public event SystemEventHandler SignedInChanged;
    public event AccountListModifiedHandler AccountListModified;
    public event AvailableStatusChangedHandler AvailableStatusChanged;
    public event InternalEventHandler InternalEvent;
    public event PreferencesUpdatedHandler PreferencesUpdated;

    private struct PluginHandler
    {
      public IPlugin Plugin;
      public Delegate Handler;

      public PluginHandler(IPlugin plugin, Delegate handler)
      {
        this.Plugin = plugin;
        this.Handler = handler;
      }
    }

    /// <summary>
    /// Encapsulates socket connection information
    /// </summary>
    class SocketConnectionData
    {
      private string _server = "";
      private int _port = 0;
      private Socket _socket = null;
      private Delegate _callback = null;

      /// <summary>
      /// Gets the server to which to connect through the proxy
      /// </summary>
      public string Server
      {
        get { return _server; }
      }

      /// <summary>
      /// Gets the port to which to connect through the proxy
      /// </summary>
      public int Port
      {
        get { return _port; }
      }

      /// <summary>
      /// Gets or sets the socket being connected
      /// </summary>
      public Socket Socket
      {
        get { return _socket; }
        set { _socket = value; }
      }

      /// <summary>
      /// Gets the callback used to return socket information
      /// </summary>
      public Delegate Callback
      {
        get { return _callback; }
      }

      /// <summary>
      /// Initializes a new SocketConnectionData object
      /// </summary>
      public SocketConnectionData(string server, int port, Delegate callback)
      {
        _server = server;
        _port = port;
        _callback = callback;
      }
    }
  }
}
